package com.wbao.dianniu.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.KnowListAdapter;
import com.wbao.dianniu.data.KnowledgeData;
import com.wbao.dianniu.listener.IKnowledgeListListener;
import com.wbao.dianniu.manager.KnowListManager;
import com.wbao.dianniu.update.KnowledgeHelper;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我发的文章
 *
 * @author yejianjun
 */
public class HeadKnowFragment extends Fragment implements IKnowledgeListListener, KnowledgeHelper.IKnowledgeChangedListenr {

    private PullToRefreshListView pullListView;
    private ListView listView;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private KnowListManager manager;
    private KnowListAdapter adapter;
    private final int TYPE = 3;
    private TextView noDataTV;
    private static Integer friendId = 0;

    public static HeadKnowFragment instance(Context context, Integer id) {
        HeadKnowFragment view = new HeadKnowFragment();
        friendId = id;
        return view;
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_i_release, null);
        initView(view);
        initData();
        KnowledgeHelper.getInstance().addListener(this);
        return view;
    }

    private void initView(View view) {
        noDataTV = (TextView) view.findViewById(R.id.no_find_data);
        pullListView = (PullToRefreshListView) view.findViewById(R.id.i_release_listview);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        listView = pullListView.getRefreshableView();

        registerForContextMenu(listView);
    }

    private void initData() {
        manager = new KnowListManager(getContext());
        manager.addKnowledgeListListener(this);
        adapter = new KnowListAdapter(getContext(), 1);
        adapter.addData(new ArrayList<KnowledgeData>());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);
        requestData(currentPage);
    }

    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            KnowledgeData data = (KnowledgeData) adapter.getItem(position - 1);
            Utils.intoKnowDetailActivity(getContext(), 0, data);
        }
    };


    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        currentPage++;
        requestData(currentPage);
    }

    private void requestData(int page) {
        manager.knowledgeList(friendId, TYPE, "", page * PAGE_COUNTS, PAGE_COUNTS);
    }

    @Override
    public void addOneKnowledge(KnowledgeData data) {

    }

    @Override
    public void removeOneKnowledge(int knowledgeId) {
        adapter.removeOneData(knowledgeId);
        if (adapter.getCount() == 0) {
            noDataTV.setVisibility(View.VISIBLE);
        } else {
            noDataTV.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void goodNotify(int knowledgeId, int isGood, int goodCount) {
        adapter.goodChanged(knowledgeId, isGood, goodCount);
    }

    @Override
    public void replyNotify(int knowledgeId, int answerCounts) {
        // TODO Auto-generated method stub
        adapter.replyNotify(knowledgeId, answerCounts);
    }

    @Override
    public void visitorNotify(int knowledgeId, int visitorCounts) {
        adapter.visitorNotify(knowledgeId, visitorCounts);
    }

    @Override
    public void modifyOneData(int knowledgeId, KnowledgeData data) {

    }


    @Override
    public void onKnowledgeListFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
        Notification.toast(getContext(), errorMsg);
    }

    @Override
    public void onKnowledgeListSuccess(List<KnowledgeData> list) {
        pullListView.onRefreshComplete(false, true);
        if (null != list) {
            adapter.addData(list);
            if (adapter.getCount() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager)
            manager.removeKnowledgeListListener(this);
        KnowledgeHelper.getInstance().removeListener(this);
    }

}
